package jxl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    private static final Log log = LogFactory.getLog(Main.class);
    private static final Scanner in = new Scanner(System.in);

    private static String storeDir = null;
    public static void main(String[] args) {
        try {
            log.info("JXL本次启动时间 ：" + toString(new Date(),null));
            System.out.println("本程序经测试，当前已支持下载视频/安装包！");
            boolean a = true;
            while (a){
                storeDir = getInput("请先设置你的文件存储目录:");
                if (storeDir.contains(":\\"))a = false;
            }
            log.info("下载文件储存目录："+storeDir);
            List<String> urlList = new ArrayList();
            boolean b = true;
            while (b) {
                String url = getInput("请输入文件链接地址(多链接可以用逗号隔开，开始下载:y):");
                if (!"y".equals(url)) {
                    if (url.contains(",")){
                        urlList = getUrlList(url,",",urlList);
                    }else if (url.contains("，")){
                        urlList = getUrlList(url,"，",urlList);
                    }else if (url.contains("http")){
                        urlList.add(url);
                    }else if (url.contains("thunder://")){
                        urlList.add(xunleiLianJieJieXi(url));
                    }
                    continue;
                }
                if (urlList.isEmpty()) {
                    System.out.println("文件链接地址不可为空");
                    continue;
                }
                b = false;
                log.info("下载链接："+urlList.toString());
                log.info("下载数量："+urlList.size());
                for (int i = 0;i<urlList.size();i++) {
                    try {
                        jxl.Job job = new jxl.Job(urlList.get(i), storeDir);
                        job.startJob(i+1);
                    }catch (Exception e){
                        log.error(e.getMessage());
                        System.out.println(e.getMessage());
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            System.out.println("文件下载失败"+e.getMessage());
        }
    }

    private static List getUrlList(String url,String rule,List urlList){
        List<String> split = Arrays.asList(url.split(rule));
        for(String str : split){
            if (str.contains("http")){
                urlList.add(str);
            }else if (url.contains("thunder://")){
                urlList.add(xunleiLianJieJieXi(url));
            }
        }
        return urlList;
    }

    private static String xunleiLianJieJieXi(String str){
        str = str.replaceAll("thunder://","");
        String s = new String(Base64.decodeBase64(str));
        return s.substring(2,s.length()-2);
    }


    private static int getIntInput(String message) throws IOException {
        String number = getInput(message);
        while(!number.matches("\\d+")) {
            System.out.println("线程数必须是1个整数");
            number = getInput(message);
        }
        return Integer.parseInt(number);
    }


    private static String getInput(String message) throws IOException {
        System.out.print(message);
        String line = in.nextLine();
        while(line == null || line.trim().length()<1) {
            System.out.print(message);
            line = in.nextLine();
        }

        return line.trim();

    }

    public static String toString(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        if (pattern == null) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        String dateString = "";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            dateString = sdf.format(date);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return dateString;
    }
}
